package com.fp.m7.client.common;

import java.util.Date;

import org.gwttime.time.DateTime;

/**
 * A helper class for defining time-span units. As for now these are: days, weeks and months. This class has methods
 * recalculating page numbers into dates: starting date & ending date for the specified time-unit.
 * 
 * @author 7
 */
public enum TimeUnit {

    DAYS(1), WEEKS(7), MONTHS(30);

    private int days;

    private TimeUnit(int days) {
        this.days = days;
    }

    /**
     * Translates provided pageNo to a date instance in reference to the time-unit this object represents.
     * 
     * @param pageNo
     *            ranges 0..n
     * @return a date of start of day which was pageNo time units ago.
     */
    public Date getFrom(int pageNo) {
        DateTime date = new DateTime().withMillisOfDay(0);
        return date.minusDays(pageNo * days).toDate();
    }

    /**
     * Translates provided pageNo to a date instance in reference to the time-unit this object represents.
     * 
     * @param pageNo
     *            ranges 0..n
     * @return a date of start of day which was (pageNo + 1) time units ago.
     */
    public Date getTo(int pageNo) {
        DateTime date = new DateTime().plusDays(1).withMillisOfDay(0);
        return date.minusDays(pageNo * days).toDate();
    }
}
